#include "../../common/message.hpp"
#include "../../common/net.hpp"
#include "../../common/dispatcher.hpp"
#include "../../server/rpc_router.hpp"

void Add(const Json::Value& req,Json::Value& rsp)
{
    int num1 = req["num1"].asInt();
    int num2 = req["num2"].asInt();
    rsp = num1 + num2;
}

int main()
{
    auto router = std::make_shared<gmrpc::server::RpcRouter>();
    std::unique_ptr<gmrpc::server::SDescribeFactory> desc_factory(new gmrpc::server::SDescribeFactory());
    desc_factory->setMethodName("Add");
    desc_factory->setParamsDesc("num1",gmrpc::server::VType::INTEGRAL);
    desc_factory->setParamsDesc("num2",gmrpc::server::VType::INTEGRAL);
    desc_factory->setReturnType(gmrpc::server::VType::INTEGRAL);
    desc_factory->setCallback(Add);
    router->registerMethod(desc_factory->build());

    auto cb = std::bind(&gmrpc::server::RpcRouter::onRpcRequest, router.get(),
                        std::placeholders::_1, std::placeholders::_2);

    auto dispatcher = std::make_shared<gmrpc::Dispatcher>();
    dispatcher->registerHandler<gmrpc::RpcRequest>(gmrpc::MType::REQ_RPC, cb);

    auto server = gmrpc::ServerFactory::create(9090);
    auto message_cb = std::bind(&gmrpc::Dispatcher::onMessage, dispatcher.get(),
                                std::placeholders::_1, std::placeholders::_2);

    server->setMessageCallback(message_cb);
    server->start();
    
    return 0;
}
